//
//  XLStatusRetweetView.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/27.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

class XLStatusRetweetView: UIView {

    private var reweetViewCon: Constraint?
    
    // 增加一个属性供外界赋值
    var reweetViewModel: XLHomeViewModel?{
        didSet{
                contentReweetedLabel.text = reweetViewModel?.retweetContent
            // 写在约束
            reweetViewCon?.uninstall()
            // 有配图
            if (reweetViewModel?.statusModel?.retweeted_status?.pic_urls?.count)! > 0{
                pictureView.pictureModel = reweetViewModel?.statusModel?.retweeted_status?.pic_urls
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    self.reweetViewCon = make.bottom.equalTo(pictureView).offset(5).constraint
                })
                layoutIfNeeded()
            }else{
                // 没有配图
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    self.reweetViewCon = make.bottom.equalTo(contentReweetedLabel).offset(5).constraint
                })
            }
            layoutIfNeeded()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 设置视图
    private func setupUI(){
//        backgroundColor = XLRandomColor()
        backgroundColor = UIColor.init(colorLiteralRed: 240.0 / 255, green: 240.0 / 255, blue: 240.0 / 255, alpha: 1.0)
        // 添加控件
        addSubview(contentReweetedLabel)
        addSubview(pictureView)
        // 设置约束
        contentReweetedLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(10)
//            make.bottom.equalTo(self).offset(-5)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentReweetedLabel.snp_bottom).offset(10)
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(contentReweetedLabel)
//            make.bottom.equalTo(self).offset(-5)
        }
        
        self.snp_makeConstraints { (make) in
            self.reweetViewCon = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //懒加载控件 
    // 转发微博正文
    private lazy var contentReweetedLabel: UILabel = UILabel(text: "", fontSize: XLNormalFontSize, textColor: UIColor.black, maxWidth: XLSCREENW - 20)
    // 转发微博配图
    private lazy var pictureView: XLStatusPictureView = {
        let view = XLStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

}
